﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIG.Model;
using SIG.Data.Repositories.Identity;
using SIG.Infrastructure.Logging;
using SIG.Model.Identity;
using System.Threading;
using SIG.Data.UnitOfWork;
using SIG.Infrastructure.Cache;
using SIG.Repository;
using SIG.Resources;
using SIG.Infrastructure.Helper;
using SIG.Resources.Admin;

namespace SIG.Service.Identity
{
    public class RoleService : EntityService<Role>, IRoleService
    {
        #region Fields
        private IRoleRepository _roleRepository;
        private IMenuRepository _menuRepository;
        private ILoggingService _logger;
        private ICacheService _cacheService;
        #endregion

        public RoleService(IRoleRepository roleRepository, ICacheService cacheService, ILoggingService logger, IMenuRepository menuRepository)       
            :base(roleRepository, cacheService, logger)     
        {
            if (roleRepository == (IRoleRepository)null)
                throw new ArgumentNullException("userRepository");
            if (menuRepository == (IRoleRepository)null)
                throw new ArgumentNullException("menuRepository");
            if (logger == (ILoggingService)null)
                throw new ArgumentNullException("logger");
            if (cacheService == (ICacheService)null)
                throw new ArgumentNullException("cacheService");
            this._roleRepository = roleRepository;
            this._logger = logger;
            this._cacheService = cacheService;
            this._menuRepository = menuRepository;
        }

        public void SetRoleMenus(int RoleId, int[] menuId)
        {
            Role vRole = _roleRepository.GetElementById(RoleId);
            vRole.Menus.Clear();
            vRole.Menus = _menuRepository.GetAllElements().Where(m => menuId.Contains(m.Id)).ToList();
                      
            this.Update(vRole);

            var key = $"{EntityNames.Menu}s";
            _cacheService.Invalidate(key); //取消缓存

        }


        //public async Task AddAsync(Role role, CancellationToken cancellationToken = default(CancellationToken))
        //{
        //    cancellationToken.ThrowIfCancellationRequested();
        //    if (role == (Role)null)
        //        throw new ArgumentNullException("role");

        //    IMainBCUnitOfWork unitOfWork = this._roleRepository.UnitOfWork as IMainBCUnitOfWork;
        //    this._roleRepository.Add(role);
        //    await unitOfWork.CommitAsync(cancellationToken);
        //}


        //public override IEnumerable<Role> GetAll()
        //{

        //    var key = "Roles_All";

        //    List<Role> roles;

        //    if (SettingsManager.Role.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(key))
        //        {
        //            roles = (List<Role>)_cacheService.Get(key);

        //        }
        //        else
        //        {
        //            roles = _roleRepository.GetAll().OrderBy(w => w.RoleName).ToList();
        //            _cacheService.Set(key, roles, SettingsManager.Role.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        roles = _roleRepository.GetAll().OrderBy(w => w.RoleName).ToList();
        //    }

        //    return roles;

        //}



        //public override void Create(Role role)
        //{
        //    try
        //    {

        //        base.Create(role);
        //        _cacheService.Invalidate("Role");
        //        _loggingService.Info(string.Format(Site.logCreateMessage, SettingsManager.Role.ModelName, role.RoleName, Site.CurrentUserName));

        //    }
        //    catch (Exception er)
        //    {
        //        var message = String.Format(Site.logErrorCreateMessage, SettingsManager.Role.ModelName);
        //        _loggingService.Error(message, er);
        //    }

        //}

        //public override void Update(Role role)
        //{
        //    try
        //    {
        //        base.Update(role);
        //        _cacheService.Invalidate("Role");
        //        _loggingService.Info(string.Format(Site.logUpdateMessage, SettingsManager.Role.ModelName, role.RoleName, Site.CurrentUserName));

        //    }
        //    catch (Exception er)
        //    {
        //        var message = String.Format(Site.logErrorUpdateMessage, SettingsManager.Role.ModelName);
        //        _loggingService.Error(message, er);
        //    }


        //}

        //public override void Delete(Role role)
        //{
        //    try
        //    {

        //        base.Delete(role);
        //        _cacheService.Invalidate("Role");
        //        _loggingService.Info(string.Format(Site.logDeleteMessage, SettingsManager.Role.ModelName, role.RoleName, Site.CurrentUserName));

        //    }
        //    catch (Exception er)
        //    {
        //        var message = String.Format(Site.logErrorDeleteMessage, SettingsManager.Role.ModelName);
        //        _loggingService.Error(message, er);
        //    }


        //}
    }
}